package com.rainbow.modules.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainbow.common.util.ResultUtil;
import com.rainbow.common.vo.PageVo;
import com.rainbow.common.vo.Result;
import com.rainbow.common.vo.SearchVo;
import com.rainbow.modules.log.entity.Log;
import com.rainbow.modules.log.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * Auth rainbow
 */
@Slf4j
@RestController
@Api(description = "日志管理接口")
@RequestMapping("/rainbow/log")
@Transactional
public class LogController {

    @Autowired
    private LogService logService;

    @GetMapping(value = "/getAllByPage")
    @ApiOperation(value = "分页获取全部")
    public Result<IPage<Log>> getAllByPage(@RequestParam(required = false) Integer type,
                                           @RequestParam String key,
                                           @ModelAttribute SearchVo searchVo,
                                           @ModelAttribute PageVo pageVo) {
        // 模糊查询
        IPage<Log> log = logService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()));
        return new ResultUtil<IPage<Log>>().setData(log);

    }

    @DeleteMapping(value = "/delByIds/{ids}")
    @ApiOperation(value = "批量删除")
    public Result<Object> delByIds(@PathVariable String[] ids) {

        for (String id : ids) {

            logService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("删除成功");
    }

    @RequestMapping(value = "/delAll", method = RequestMethod.DELETE)
    @ApiOperation(value = "全部删除")
    public Result<Object> delAll() {
        logService.remove(new QueryWrapper<>());
        return new ResultUtil<Object>().setSuccessMsg("删除成功");
    }
}
